// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ATIOmega85Agent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Hardware.Runtime.Sensors
{
    using System;
    using System.Runtime.Serialization;

    using Microsoft.Robotics.Hardware.Sensors.ATI;
    using Microsoft.Robotics.Manipulation.Runtime.Sensor;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;

    /// <summary>
    /// An agent that talks to an ATI Omega85 force torque sensor
    /// </summary>
    [DataContract]
    [System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Design",
    "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",
    Justification = "Agent handles disposal of unmanaged resources in OnDeactivate()")]
    public class ATIOmega85Agent : ConsumerProducerAgent<TimerMessage, ForceTorqueMessage>
    {
        /// <summary>
        /// IP address of the force torque sensor
        /// </summary>
        [DataMember]
        private string ipAddress;

        /// <summary>
        /// Force torque sensor
        /// </summary>
        private ATIOmega85 forceTorqueSensor;

        /// <summary>
        /// Initializes a new instance of the <see cref="ATIOmega85Agent"/> class.
        /// </summary>
        /// <param name="name">Name of this agent</param>
        /// <param name="heartbeatAgent">Heartbeat agent</param>
        /// <param name="ipAddress">IP address of the sensor</param>
        public ATIOmega85Agent(string name, IProducer<TimerMessage> heartbeatAgent, string ipAddress)
            : base(name, heartbeatAgent.Name)
        {
            this.ipAddress = ipAddress;
        }

        /// <summary>
        /// Initializes the agent
        /// </summary>
        /// <param name="locator">Agent locator</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
            this.forceTorqueSensor = new ATIOmega85(this.ipAddress);
        }

        /// <summary>
        /// Start streaming on activation
        /// </summary>
        public override void OnActivated()
        {
            base.OnActivated();
            this.forceTorqueSensor.StartStreaming();
        }

        /// <summary>
        /// Stop streaming on deactivation
        /// </summary>
        public override void OnDeactivated()
        {
            base.OnDeactivated();
            this.forceTorqueSensor.StopStreaming();
            this.forceTorqueSensor.Dispose();
        }

        /// <summary>
        /// Publishes a force torque message at each heartbeat
        /// </summary>
        /// <param name="message">A heart beat message</param>
        public override void Receive(TimerMessage message)
        {
            if (this.forceTorqueSensor.IsActive)
            {
                Vector3 force;
                Vector3 torque;
                this.forceTorqueSensor.GetCurrentForceTorque(out force, out torque);
                ForceTorqueMessage forceTorqueMsg = new ForceTorqueMessage(force, torque);
                this.Publisher.Post(forceTorqueMsg);
            }
        }
    }
}
